﻿namespace TVersityDotNet.MediaLib
{
    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Xml.Linq;

    [DataContractAttribute]
    public class MediaLibMediaItem : MediaLibResponseItem
    {
        [DataMember]
        public Uri Icon { get; set; }
        [DataMember]
        public long Size { get; set; }
        [DataMember]
        public string ProtocolInfo { get; set; }
        protected XElement ResElement { get; set; }

        public string ResString
        {
            get
            {
                return this.ResElement.Value;
            }            
        }

        [DataMember]
        public Uri ItemUri
        {
            get { return new Uri(this.ResString); }
            private set { }
        }

        public MediaLibMediaItem(XElement xmlElement)
        {
            this.SearchClass = MediaLibClassType.MediaItem;
            this.RawXml = xmlElement;
            this.ProcessXmlElement();
        }
        public MediaLibMediaItem()
        {
            this.SearchClass = MediaLibClassType.NoClass;
            this.RawXml = null;            
        }

        public new void ProcessXmlElement()
        {            
            base.ProcessXmlElement();
            this.ResElement = this.RawXml.Element(XName.Get("res", MediaLibResponseItem.Namespace_Base));            
            this.Icon = new Uri(this.RawXml.Element(XName.Get("icon", MediaLibResponseItem.Namespace_Upnp)).Value);
            if (this.ResElement.Attribute("size") != null)
                this.Size = long.Parse(this.ResElement.Attribute("size").Value, CultureInfo.InvariantCulture);
            else
                this.Size = 0;
            this.ProtocolInfo = this.ResElement.Attribute("protocolInfo").Value;
        }
    }

}
